package com.czd.support.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.model.Paginate;
import com.czd.base.utils.WebUtils;
import com.czd.support.model.Payee;
import com.czd.support.model.Project;
import com.czd.support.model.Team;
import com.czd.support.service.PayeeService;
import com.czd.support.service.ProjectService;
import com.czd.support.service.TeamService;

/**
 * 
 * @Description  TODO
 * @Author JinMa.Xiao
 * @Date    2015年5月27日 下午4:16:06
 * @Version v1.0
 */
@Controller
@RequestMapping("/project")
public class ProjectController   extends   BaseController {
	@Autowired
	private  ProjectService  projectService;
	
	@Autowired
	private  PayeeService  payeeService;
	
	@Autowired
	private  TeamService  teamService;

	/**
	 * 查出所有项目的列表
	 * @param model
	 * @param request
	 * @param project
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value = {"","/","add"},method = RequestMethod.GET)
	public String list(Model model,HttpServletRequest request,Project project,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
		String kw = WebUtils.findParamStr(request,"kw");
		project.setPageNo(pageNo);
		Paginate queryForPaginate = projectService.queryForPaginate(project,kw);
		model.addAttribute("paginate",queryForPaginate);
		return "project/show";//跳到页面
	}
	 
	/**
	 * 查看详情信息 编辑
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/more",method = RequestMethod.GET)
	public String change(@PathVariable int id,Model model){
		Team team = teamService.findById(id);
		model.addAttribute("team",team);
	    	  
		//查出project详情
		Project project = projectService.findById(Integer.parseInt(id+""));
		model.addAttribute("project",project);
	  	  
		//收款人信息
		Payee payee = payeeService.findById(id);
		model.addAttribute("payee",payee);
		return "project/more";//跳到页面    //变网址
	} 
	
	/**
	 * 项目审核
	 * @param id
	 * @param editStatus
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/approval/{id}/{editStatus}")
	public String approval(@PathVariable int id, @PathVariable int editStatus, Model model){
		Project project = projectService.findById(id);
		project.setEditStatus(editStatus);
		if(projectService.updateEditStatus(project)){
			return "redirect:/project/add";
		}
		return error(model, "审核失败", "审核失败");
	}
}               
